import Vue from 'vue'
import aWrap from './a-wrap.vue'
import aDialog from './a-dialog.vue'
import aForm from './a-form.vue'
import aFormDialog from './a-form-dialog.vue'
import aSearch from './a-search.vue'
import aTable from './a-search.vue'
import aPagination from './a-pagination.vue'
import aRightTool from './a-right-tool.vue'
// import test from "./test";

export function install () {
  Vue.component('aWrap', aWrap)
  Vue.component('aDialog', aDialog)
  Vue.component('aForm', aForm)
  Vue.component('aFormDialog', aFormDialog)
  Vue.component('aSearch', aSearch)
  Vue.component('aTable', aTable)
  Vue.component('aPagination',aPagination)
  Vue.component('aRightTool',aRightTool)
}

export function $jsonParse(e) {
  try {
    return JSON.parse(e)
  } catch (err) {
    return undefined
  }
}

export function $jsonStringify(e) {
  try {
    return JSON.Stringify(e)
  } catch (err) {
    return undefined
  }
}
/**
 * @author:YYanan
 * @desc:校验身份证
 * @date: 2024-11-04 16:15:47
 */

const ckeckIdCard = (rule, value, callback) => {
  if (!value && rule.required) {
    callback(new Error('请输入'+ rule.label));
  } else {
    if(!/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(value)){
      callback(rule.label + '格式错误');
    }
    callback();
  }
};

const checkPassword = (rule, value, callback) => {
  if (!value  && rule.required) {
    callback(new Error('请输入'+ rule.label));
  } else {
    if(!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{10,}$/.test(value)){
      callback(rule.label + '至少10位数,包含大小写字母,数字');
    }
  }
  callback();
};

const checkRepassword = (rule, value, callback) => {
  if (!value  && rule.required) {
    callback(new Error('请输入'+ rule.label));
  } else {
    if(!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/.test(value)){
      callback(rule.label + '至少8位数,包含大小写字母,数字');
    }
    if(rule.password !== value){
      callback('两次密码输入不一致');
    }
  }
  callback();
};
/**
 * @author:YYanan
 * @desc:校验银行卡
 * @date: 2024-11-04 16:16:16
 */

const checkPostCard = (rule, value, callback) => {
  console.log('银行卡校验rule',rule)
  if (!value  && rule.required) {
    callback(new Error('请输入'+  rule.label));
  } else {
    if(!/^\d{16,19}$/.test(value)){
      callback(rule.label + '格式错误');
    }
  }
  callback();
};

const checkPhone = (rule, value, callback) => {
  const phoneReg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
  if (!value) {
    callback(new Error('请输入' + rule.label))
  }
  if (!Number.isInteger(+value)) {
    callback(new Error('请输入数字值'))
  } else {
    if (phoneReg.test(value)) {
      callback()
    } else {
      callback(new Error('手机号码格式不正确'))
    }
  }
}

const checkEmail = (rule, value, callback) => {
  if (!value) {
    callback(new Error('邮箱不能为空'))
  }
  if (/^[a-zA-Z0-9](?:[a-zA-Z0-9._%+-]*[a-zA-Z0-9])?@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,64}$/.test(value)) {
    callback()
  } else {
    callback(new Error('邮箱格式不正确'))
  }
}



const downloadImg = (src) =>{
    const link = document.createElement('a');
    link.href = src;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

export default {
  install,
  $jsonParse,
  checkPhone,
  checkPostCard,
  ckeckIdCard,
  downloadImg,
  checkEmail,
  checkPassword
}
